package com.hsjk.szwj.module.system.service.poster.impl;

import cn.hutool.core.collection.CollUtil;
import com.hsjk.szwj.framework.common.constant.CommonConstant;
import com.hsjk.szwj.framework.common.pojo.PageResult;
import com.hsjk.szwj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hsjk.szwj.module.system.controller.admin.poster.vo.AdminCreatePosterReqVO;
import com.hsjk.szwj.module.system.controller.admin.poster.vo.AdminPosterDetailRespVO;
import com.hsjk.szwj.module.system.controller.admin.poster.vo.AdminPosterPageReqVO;
import com.hsjk.szwj.module.system.controller.admin.poster.vo.AdminPosterPageRespVO;
import com.hsjk.szwj.module.system.controller.admin.poster.vo.AdminUpdatePosterReqVO;
import com.hsjk.szwj.module.system.controller.app.poster.vo.AppPosterDetailRespVO;
import com.hsjk.szwj.module.system.controller.app.poster.vo.AppPosterPageReqVO;
import com.hsjk.szwj.module.system.controller.app.poster.vo.AppPosterPageRespVO;
import com.hsjk.szwj.module.system.convert.poster.PosterConvert;
import com.hsjk.szwj.module.system.dal.dataobject.poster.PosterConfigDO;
import com.hsjk.szwj.module.system.dal.mysql.poster.IPosterConfigMapper;
import com.hsjk.szwj.module.system.service.poster.IPosterConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/12/11 15:08
 **/
@Service
public class PosterConfigImpl implements IPosterConfig {
    @Resource
    private IPosterConfigMapper posterConfigMapper;

    @Override
    public Boolean createPoster(AdminCreatePosterReqVO reqVO) {
        PosterConfigDO posterConfigDO = PosterConvert.INSTANCE.convertPosterConfigDO(reqVO);
        int insert = posterConfigMapper.insert(posterConfigDO);
        return insert > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean updatePoster(AdminUpdatePosterReqVO reqVO) {
        PosterConfigDO posterConfigDO = PosterConvert.INSTANCE.convertPosterConfigDO(reqVO);
        int update = posterConfigMapper.updateById(posterConfigDO);
        return update > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public AdminPosterDetailRespVO getAdminPosterDetail(Long id) {
        PosterConfigDO posterConfigDO = posterConfigMapper.selectById(id);
        return PosterConvert.INSTANCE.convertAdminPosterDetailRespVO(posterConfigDO);
    }

    @Override
    public PageResult<AdminPosterPageRespVO> getAdminPosterPage(AdminPosterPageReqVO reqVO) {
        PageResult<PosterConfigDO> posterConfigDOPageResult = posterConfigMapper.selectPage(reqVO);
        return PosterConvert.INSTANCE.convertAdminPosterPageResp(posterConfigDOPageResult);
    }

    @Override
    public Boolean deletePoster(Long id) {
        int result = posterConfigMapper.deleteById(id);
        return result > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean setPoster(Long id) {
        List<PosterConfigDO> posterConfigDOS = posterConfigMapper.selectList(new LambdaQueryWrapperX<>());
        List<PosterConfigDO> updateList = new ArrayList<>();
        Long checkId = 0L;
        if (CollUtil.isNotEmpty(posterConfigDOS)) {
            for (PosterConfigDO configDO : posterConfigDOS) {
                if (id.equals(configDO.getId())) {
                    checkId = configDO.getId();
                }
                PosterConfigDO updateItem = new PosterConfigDO();
                updateItem.setId(configDO.getId());
                updateItem.setIfSwitch(CommonConstant.NO);
                updateList.add(updateItem);
            }
            if (CollUtil.isNotEmpty(updateList)) {
                posterConfigMapper.updateBatch(updateList, updateList.size());
            }
            //设置选中的海报
            if (checkId > 0) {
                PosterConfigDO checkItem = new PosterConfigDO();
                checkItem.setId(checkId);
                checkItem.setIfSwitch(CommonConstant.YES);
                posterConfigMapper.updateById(checkItem);
            }
        }
        return Boolean.TRUE;
    }

    @Override
    public PageResult<AppPosterPageRespVO> getAppPosterPage(AppPosterPageReqVO reqVO) {
        PageResult<PosterConfigDO> posterConfigDOPageResult = posterConfigMapper.selectPage(reqVO);
        return PosterConvert.INSTANCE.convertAppPosterPageResp(posterConfigDOPageResult);
    }

    @Override
    public AppPosterDetailRespVO getAppSetPoster() {
        PosterConfigDO posterConfigDO = posterConfigMapper.selectOne(new LambdaQueryWrapperX<PosterConfigDO>()
                .eq(PosterConfigDO::getIfSwitch, CommonConstant.YES));
        return PosterConvert.INSTANCE.convertAppPosterDetailRespVO(posterConfigDO);
    }
}
